package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.enums.HuanXinMessageType;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TanHuaService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private QuestionService questionService;

    @Reference(version = "1.0.0")
    private HuanXinApi huanXinApi;

    @Reference(version = "1.0.0")
    private VisitorsApi visitorsApi;

    /**
     * 查看佳人信息
     *
     * @param userId
     * @return
     */
    public TodayBest findBestUserInfo(Long userId) {

        UserInfo userInfo = userInfoService.findUserInfo(userId);

        if (ObjectUtil.isEmpty(userInfo)) {
            return null;
        }
        TodayBest todayBest = new TodayBest();
        todayBest.setId(userId);
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setAge(userInfo.getAge());
        todayBest.setGender(userInfo.getSex().name().toLowerCase());
        todayBest.setTags(userInfo.getTags().split(","));
        todayBest.setAvatar(userInfo.getLogo());
        //缘分值
        User user = UserThreadLocal.get();
        todayBest.setFateValue(recommendUserService.findScore(userId, user.getId()).longValue());

        //记录来访用户
        visitorsApi.saveVisitors(userId,user.getId(),"个人主页");

        return todayBest;
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    public String findStrangerQuestion(Long userId) {
        Question question = questionService.findQuestion(userId);
        if (ObjectUtil.isNotEmpty(question)) {
            return question.getTxt();
        }
        //设置一个默认的问题
        return "你的爱好是什么？";
    }

    /**
     * 回复陌生人问题
     * @param userId
     * @param reply
     * @return
     */
    public Boolean answerQuestion(Long userId, String reply) {
        User user = UserThreadLocal.get();
        UserInfo userInfo = userInfoService.findUserInfo(user.getId());
        //构建消息内容
        //{"userId":1,"huanXinId":"HX_1","nickname":"黑马小妹",
        // "strangerQuestion":"你喜欢去看蔚蓝的大海还是去爬巍峨的高山？",
        // "reply":"我喜欢秋天的落叶，夏天的泉水，冬天的雪地，只要有你一切皆可~"}
        Map<String,Object> msg = new HashMap<>();
        msg.put("userId",user.getId());
        msg.put("huanXinId","HX_"+user.getId());
        msg.put("nickname",userInfo.getNickName());
        msg.put("strangerQuestion",findStrangerQuestion(userId));
        msg.put("reply",reply);
        //发送环信消息
        return huanXinApi.answerQuestion("HX_"+userId,HuanXinMessageType.TXT, JSONUtil.toJsonStr(msg));
    }
}
